package com.blog.controller;

import com.blog.pojo.Article;
import com.blog.pojo.Tag;
import com.blog.service.ArticleService;
import com.blog.service.TagService;
import com.blog.service.impl.ArticleServiceImpl;
import com.blog.service.impl.TagServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 首页的控制器  作用显示首页的数据
 */
@WebServlet("/IndexServlet.do")
public class IndexServlet extends HttpServlet {

    ArticleService articleService=new ArticleServiceImpl();

    TagService tagService=new TagServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询最近的10个文章，首页显示
        List<Article> top10Articles = articleService.getTop10Articles();
        //传值
        req.setAttribute("article_list",top10Articles);

        //查询文章分类以及该分类下文章数
        Map<String, Integer> articleSortAndCount = articleService.getArticleSortAndCount();
        req.setAttribute("sort_count_map",articleSortAndCount);

        //查询阅读排行榜
        List<Article> top10VisitArticles = articleService.getTop10VisitArticles();
        req.setAttribute("visit_rank",top10VisitArticles);

        //文章标签
        List<Tag> tags = tagService.getTags();
        req.setAttribute("tag_list",tags);

        //转发首页
        req.getRequestDispatcher("/page/main.jsp").forward(req,resp);
    }
}
